package com.example.fuzhuang.services;

import com.example.fuzhuang.dto.Product;
import com.example.fuzhuang.dto.ProductExample;
import com.example.fuzhuang.mapper.ProductMapper;
import com.example.fuzhuang.mapper.ProductMyMapper;
import com.example.fuzhuang.req.ProductReq;
import com.example.fuzhuang.resp.PageResp;
import com.example.fuzhuang.resp.ProductResp;
import com.example.fuzhuang.resp.SumInventoryResp;
import com.example.fuzhuang.utils.CopyUtil;
import com.example.fuzhuang.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductService {
    private static final Logger LOG = LoggerFactory.getLogger(ProductService.class);

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductMyMapper productMyMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<ProductResp> list(ProductReq req) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<Product> productList = productMapper.selectByExample(productExample);

        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        PageResp<ProductResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());

        List<ProductResp> productRespList = CopyUtil.copyList(productList,ProductResp.class);

        pageResp.setList(productRespList);
        return pageResp;
    }

    public PageResp<ProductResp> myList(ProductReq req) {
        PageHelper.startPage(req.getPage(),req.getSize());

        List<ProductResp> productList = productMyMapper.selectMyProducts(req.getName(),req.getCode(),req.getFirstType(),req.getSecondType());

        PageInfo<ProductResp> pageInfo = new PageInfo<>(productList);
        PageResp<ProductResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());

        pageResp.setList(productList);

        return pageResp;
    }

    public List<ProductResp> getProduct(ProductReq req){
        List<ProductResp> productList = productMyMapper.selectMyProduct(req.getCode());
//        List<ProductResp> productRespList = CopyUtil.copyList(productList,ProductResp.class);
        return productList;
    }

    public List<SumInventoryResp> getSumInventory(ProductReq req){
        return productMyMapper.SumInventory();
    }

    public ProductResp inventoryOutput(ProductReq req){
        ProductResp productResp = productMyMapper.updateInventoryOutPut(req.getInventory(),req.getId());;
        return productResp;
    }

    public ProductResp inventoryInput(ProductReq req){
        ProductResp productResp = productMyMapper.updateInventoryInPut(req.getInventory(),req.getId());
        return productResp;
    }

    public Product save(ProductReq req){

        Product product = CopyUtil.copy(req, Product.class);
        if(req.getId() == null){
//            ProductExample productExample = new ProductExample();
//            ProductExample.Criteria criteria = productExample.createCriteria();
//            if(req.getName() != null) {
//                criteria.andNameEqualTo(req.getName());
//            }
//            if(req.getFirstType() != null) {
//                criteria.andFirstTypeEqualTo(req.getFirstType());
//            }
//            if(req.getSecondType() != null) {
//                criteria.andSecondTypeEqualTo(req.getSecondType());
//            }

//            List<Product> productList = productMapper.selectByExample(productExample);

//            if(productList.size() > 0){
//                throw new BusinessException(BusinessExceptionCode.SAME_PRODUCT_ERROR);
//            }else {
//
//            }

            Product maxCodeProduct = productMyMapper.selectByMaxCode();

            if(maxCodeProduct != null){
                product.setCode(maxCodeProduct.getCode()+1);
            }else{
                Long code = 6900000000001L;
                product.setCode(code);
            }

            Long id = snowFlake.nextId();
            product.setId(id);
            productMapper.insert(product);



        }else {
            productMapper.updateByPrimaryKeySelective(product);
        }

        return product;

    }

    public void delete(ProductReq req){
        productMapper.deleteByPrimaryKey(req.getId());
    }

}
